package com.yang.service.impl;

import com.yang.constant.ConfiConstant;
import com.yang.dao.DataStatisticeDao;
import com.yang.entity.*;
import com.yang.pojo.GuestDto;
import com.yang.pojo.OrderDto;
import com.yang.service.DataStatisticsService;
import com.yang.util.PublicUtil;
import org.jboss.logging.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DataStatisticServiceImpl implements DataStatisticsService {

    private static Logger logger = Logger.getLogger(DataStatisticServiceImpl.class);

    @Resource
    DataStatisticeDao dataStatisticeDao;

    @Override
    public Map<String, Object> orderSelect(long beginTime, long overTime) {
        Map<String, Object> map = new HashMap<String, Object>();

        //判断日期是否正确
        if (beginTime > overTime) {
            map.put("status", "error");
            map.put("info", "日期错误！");
            logger.error("日期错误");
            return map;
        }

        //找到orderForm表
        List<OrderFormEntity> orderFormEntities = dataStatisticeDao.selectOrderByTime(beginTime, overTime);
        logger.info("begintime = " + beginTime + "  overTime = " + overTime);
        if (orderFormEntities == null || orderFormEntities.size() == 0) {
            map.put("status", "error");
            map.put("info", "未找到该时间段的订单消息！");
            logger.error("未找到该时间段的订单消息");
            return map;
        }

        logger.info("1");
        List<OrderDto> orderDtos = new ArrayList<OrderDto>();

        float totelneedCollect = 0;
        float totelTrueCollect = 0;

        for (OrderFormEntity orderFormEntity : orderFormEntities) {
            OrderDto orderDto = new OrderDto();
            //订单编号、应收、实收、租房时长
            orderDto.setOrderId(String.valueOf(orderFormEntity.getId()));
            orderDto.setRantal(orderFormEntity.getRantal());
            orderDto.setTruePay(orderFormEntity.getPay());
            orderDto.setStayDay(orderFormEntity.getStayDay());

            //计算总应收和实收
            totelneedCollect += orderFormEntity.getRantal();
            totelTrueCollect += orderFormEntity.getPay();

            //转换时间
            orderDto.setBeginDay(PublicUtil.chargeMillisToDay(orderFormEntity.getBeginDay()));
            logger.info("3");

            RelationEntity relationEntity = dataStatisticeDao.selectRelationByOrderIdAndGuestStatic(orderFormEntity.getId(), ConfiConstant.HOMEOWNER);

            if (relationEntity == null) {
                logger.error("orderId 找不到live表  orderFormEntity = " + orderFormEntity.toString());
                map.put("status", "error");
                map.put("info", "未找到该时间段的订单消息！");
                logger.error("未找到该时间段的订单消息");
                return map;
            }
            orderDto.setRoomNum(relationEntity.getRoomId().getRoomNum());
            orderDtos.add(orderDto);
        }
        map.put("status", "suecceed");
        map.put("info", orderDtos);
        map.put("totelneedCollect", totelneedCollect);
        map.put("totelTrueCollect", totelTrueCollect);
        return map;
    }

    @Override
    public Map<String, Object> guestSelect(long beginTime, long overTime) {
        Map<String, Object> map = new HashMap<String, Object>();

        //判断日期是否正确
        if (beginTime > overTime) {
            map.put("status", "error");
            map.put("info", "日期错误！");
            logger.error("日期错误");
            return map;
        }

        //找到orderForm表
        List<OrderFormEntity> orderFormEntities = dataStatisticeDao.selectOrderByTime(beginTime, overTime);
        if (orderFormEntities == null || orderFormEntities.size() == 0) {
            map.put("status", "error");
            map.put("info", "未找到该时间段的订单消息！");
            logger.error("未找到该时间段的订单消息");
            return map;
        }

        List<GuestDto> guestDtos = new ArrayList<GuestDto>();

        for (OrderFormEntity orderFormEntity : orderFormEntities) {
            RelationEntity relationEntity = dataStatisticeDao.selectRelationByOrderIdAndGuestStatic(orderFormEntity.getId(), ConfiConstant.HOMEOWNER);
            if (relationEntity == null) {
                logger.error("orderId 找不到live表  orderFormEntity = " + orderFormEntity.toString());
                map.put("status", "error");
                map.put("info", "未找到该时间段的订单消息！");
                logger.error("未找到该时间段的订单消息");
                return map;
            }
            GuestEntity guestEntity = relationEntity.getGuestId();
            GuestDto guestDto = new GuestDto();
            guestDto.setName(guestEntity.getName());
            guestDto.setMoney(orderFormEntity.getPay());
            guestDto.setIdentity(guestEntity.getIdetifcation());
            guestDto.setName(guestDto.getName());
            guestDto.setTel(guestEntity.getTel());
            guestDto.setRoomNum(relationEntity.getRoomId().getRoomNum());
            guestDtos.add(guestDto);
        }
        for (int i = 0; i < guestDtos.size() - 1; i++) {
            for (int j = i + 1; j < guestDtos.size(); j++) {
                if (guestDtos.get(i).getName().equals(guestDtos.get(j).getName()) && guestDtos.get(i).getIdentity().equals(guestDtos.get(j).getIdentity())) {
                    int times = guestDtos.get(i).getTimes();
                    guestDtos.get(i).setTimes(times++);
                    guestDtos.remove(j);
                }
                logger.info(guestDtos.get(i).toString());
            }
        }
        map.put("status", "sueeccd");
        map.put("info", guestDtos);
        return map;
    }
}
